package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.News;
import com.example.entity.NewsContent;
import com.example.entity.Rank;
import com.example.qo.NewsQuery;
import com.example.qo.RankQuery;
import com.example.redis.service.INewsRedisService;
import com.example.service.INewsContentService;
import com.example.service.INewsService;
import com.example.service.IRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2023-04-13
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {

    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsContentService newsContentService;
    @Autowired
    private INewsRedisService newsRedisService;
    @Autowired
    private IRankService rankService;
    @RequestMapping("/page")
    public Page<News> page(NewsQuery qo){
        return newsService.queryPage(qo);
    }

    @RequestMapping("/detail")
    public News detail(Long id){
        News news = newsService.getById(id);
        NewsContent content = newsContentService.getById(id);
        news.setContent(content);
        //阅读数+1
        Long viewNum = newsRedisService.viewnumIncr(id);
        news.setViewnum(viewNum);
        return news;
    }

    @RequestMapping("/persistence")
    public boolean persistence(){
        return newsRedisService.persistence();
    }

    @RequestMapping("/rank")
    public Page<News> rank(RankQuery qo,NewsQuery nqo){
        return rankService.queryPage(qo,nqo);
    }

    @RequestMapping("/historyRank")
    public List<Map<String, Object>> historyRank(NewsQuery nqo){
        return newsService.queryRanks(nqo);
    }



}

